home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVMSXF.C < prev    next >
C/C++ Source or Header  |  1993-05-26  |  14KB  |  438 lines

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevmsxf.c */
  20. /* External font (xfont) implementation for Microsoft Windows. */
  21. #include "ctype_.h"
  22. #include "math_.h"
  23. #include "memory_.h"
  24. #include "string_.h"
  25. #include "gdevmswn.h"
  26. #include "gsutil.h"
  27. #include "gxxfont.h"
  28.  
  29. /* Imported from gdevwdib.c or gdevwddb.c */
  30. extern gx_device gs_mswin_device;
  31.  
  32. /* Imported from gdevemap.c */
  33. extern const byte far_data gs_map_std_to_iso[256];
  34.  
  35. /* Declare the xfont procedures */
  36. private xfont_proc_lookup_font(win_lookup_font);
  37. private xfont_proc_char_xglyph(win_char_xglyph);
  38. private xfont_proc_char_metrics(win_char_metrics);
  39. private xfont_proc_render_char(win_render_char);
  40. private xfont_proc_release(win_release);
  41. private gx_xfont_procs win_xfont_procs = {
  42.     win_lookup_font,
  43.     win_char_xglyph,
  44.     win_char_metrics,
  45.     win_render_char,
  46.     win_release
  47. };
  48.  
  49. /* Return the xfont procedure record. */
  50. gx_xfont_procs *
  51. win_get_xfont_procs(gx_device *dev)
  52. {    return &win_xfont_procs;
  53. }
  54.  
  55. /* Define a Windows xfont. */
  56. typedef struct win_xfont_s win_xfont;
  57. struct win_xfont_s {
  58.     gx_xfont_common common;
  59.     LOGFONT lf;
  60.     TEXTMETRIC tm;
  61.     HFONT hFont;
  62.     gx_device_win *dev;                /* for GetDC */
  63.     int invert_y;
  64.     int y_offset;
  65. };
  66. #define wxf ((win_xfont *)xf)
  67.  
  68. /* Forward references */
  69. private HDC near win_get_dc(P1(gx_device_win *));
  70. private void near win_release_dc(P2(gx_device_win *, HDC));
  71. private int win_select_font(P2(HDC, win_xfont *));
  72.  
  73. /* Map from PostScript to Windows character codes. */
  74. /* (These tables were generated by winmaps.ps.) */
  75.  
  76. private const byte far_data gs_map_symbol_to_oem[256] = {
  77.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  78.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  79.   32, 33, 0, 35, 0, 37, 38, 0, 40, 41, 0, 43, 44, 0, 46, 47,
  80.   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  81.   0, 0, 0, 0, 0, 0, 231, 225, 0, 0, 0, 0, 0, 0, 0, 0,
  82.   226, 232, 0, 227, 0, 0, 0, 233, 0, 0, 0, 91, 0, 93, 0, 95,
  83.   0, 223, 224, 0, 234, 0, 236, 0, 0, 0, 0, 0, 0, 229, 0, 0,
  84.   0, 0, 0, 228, 230, 0, 0, 0, 0, 0, 0, 123, 124, 125, 0, 0,
  85.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  86.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  87.   0, 0, 0, 242, 0, 235, 0, 5, 4, 3, 6, 29, 27, 24, 26, 25,
  88.   247, 240, 0, 241, 0, 0, 0, 7, 245, 0, 239, 0, 0, 0, 0, 0,
  89.   0, 0, 0, 0, 0, 0, 0, 238, 0, 0, 0, 0, 0, 0, 237, 0,
  90.   0, 0, 0, 0, 0, 0, 250, 248, 169, 0, 0, 0, 0, 0, 0, 0,
  91.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  92.   0, 0, 0, 243, 0, 244, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  93. };
  94.  
  95. private const byte far_data gs_map_iso_to_oem[256] = {
  96.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  97.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  98.   32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 0, 46, 47,
  99.   48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  100.   64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  101.   80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  102.   96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  103.   112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 0,
  104.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  105.   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  106.   32, 172, 154, 155, 0, 156, 0, 21, 0, 0, 165, 173, 169, 45, 0, 0,
  107.   247, 240, 252, 0, 0, 229, 20, 249, 0, 0, 166, 174, 171, 170, 0, 167,
  108.   0, 0, 0, 0, 141, 142, 145, 128, 0, 143, 0, 0, 0, 0, 0, 0,
  109.   0, 164, 0, 0, 0, 0, 152, 0, 0, 0, 0, 0, 153, 0, 0, 0,
  110.   133, 159, 131, 0, 132, 134, 144, 135, 138, 130, 136, 137, 140, 160, 0, 139,
  111.   0, 163, 148, 161, 146, 0, 147, 245, 0, 150, 162, 149, 129, 0, 0, 151
  112. };
  113.  
  114. /* Correlate PostScript font names with Windows font names. */
  115. /* This table should be an external resource, like Fontmap, */
  116. /* but that will have to wait till later. */
  117.  
  118. typedef struct font_entry_s {
  119.     const char _ds *key;
  120.     const char _ds *value;
  121.     uint pitchAndFamily;
  122. } font_entry;
  123.  
  124. private const font_entry font_names[] = {
  125.     {"Courier", "Courier New", FIXED_PITCH | FF_MODERN},
  126.     {"Helvetica", "Arial", VARIABLE_PITCH | FF_SWISS},
  127.     {"Helvetica", "Helv", VARIABLE_PITCH | FF_SWISS},
  128.     {"Times", "Times New Roman", VARIABLE_PITCH | FF_ROMAN},
  129.     {"Times", "Tms Rmn", VARIABLE_PITCH | FF_ROMAN}
  130. };
  131.  
  132. /* Look up a font. */
  133. private int /*bool*/ map_logical_font(P2(HDC, win_xfont *));
  134. gx_xfont *
  135. win_lookup_font(gx_device *dev, const byte *fname, uint len,
  136.   int encoding_index, const gs_uid *puid, const gs_matrix *pmat,
  137.   const gs_memory_procs *mprocs)
  138. {    win_xfont f;
  139.     win_xfont *wf;
  140.     uint name_len = min(len, LF_FACESIZE-1);
  141.     const font_entry _ds *pfe;
  142.     HDC hdc;
  143.     /* Only handle simple cases for now. */
  144.     if ( pmat->xy != 0 || pmat->yx != 0 || pmat->xx <= 0 ||
  145.          fabs(fabs(pmat->yy) - pmat->xx) > 0.00002
  146.        )
  147.         return NULL;
  148.     f.lf.lfHeight = pmat->xx * 1000;
  149.     if ( f.lf.lfHeight <= 0 || f.lf.lfHeight >= 36 )
  150.         return NULL;
  151.     f.lf.lfWidth = 0;
  152.     f.lf.lfEscapement = 0;
  153.     f.lf.lfOrientation = 0;
  154.     f.lf.lfWeight =
  155.         (string_match(fname, len, "*Bold*", 6, NULL) ?
  156.          FW_BOLD : FW_REGULAR);
  157.     f.lf.lfItalic =
  158.         string_match(fname, len, "*Italic*", 8, NULL) ||
  159.         string_match(fname, len, "*Oblique*", 9, NULL);
  160.     f.lf.lfUnderline = 0;
  161.     f.lf.lfStrikeOut = 0;
  162.     f.lf.lfCharSet =
  163.         (encoding_index == 2 ? SYMBOL_CHARSET : ANSI_CHARSET);
  164.     f.lf.lfOutPrecision = OUT_CHARACTER_PRECIS;
  165.     f.lf.lfClipPrecision = CLIP_STROKE_PRECIS;
  166.     f.lf.lfQuality = PROOF_QUALITY;
  167.     f.hFont = 0;
  168.     f.invert_y = pmat->yy >= 0;
  169.     hdc = win_get_dc(wdev);
  170.     if ( hdc == NULL )
  171.         return NULL;
  172.     for ( pfe = font_names; pfe != &font_names[countof(font_names)]; pfe++ )
  173.       if ( !strncmp(pfe->key, fname, strlen(pfe->key)) )
  174.     {    /* Found a match. */
  175.         strcpy(f.lf.lfFaceName, pfe->value);
  176.         f.lf.lfPitchAndFamily = pfe->pitchAndFamily;
  177.         if ( map_logical_font(hdc, &f) )
  178.             break;
  179.     }
  180.     if ( f.hFont == 0 )
  181.     {    /* No matches in the table, try with the given name. */
  182.         uint len;
  183.         memcpy(f.lf.lfFaceName, fname, name_len);    /* default */
  184.         for ( len = 0; len < name_len; len++ )
  185.           if ( !isalnum(fname[len]) )
  186.             break;
  187.         f.lf.lfFaceName[len] = 0;
  188.         f.lf.lfPitchAndFamily = 0;    /* default */
  189.         if ( !map_logical_font(hdc, &f) )
  190.         {    win_release_dc(wdev, hdc);
  191.             return NULL;
  192.         }
  193.     }
  194.     GetTextMetrics(hdc, &f.tm);
  195.     win_release_dc(wdev, hdc);
  196.     f.y_offset = (!f.invert_y ? f.tm.tmAscent : f.tm.tmDescent);
  197.     wf = (win_xfont *)(*mprocs->alloc)(1, sizeof(win_xfont), "win_lookup_font");
  198.     if ( wf == 0 )
  199.     {    DeleteObject(f.hFont);
  200.         return NULL;
  201.     }
  202.     f.common.procs = &win_xfont_procs;
  203.     f.dev = wdev;
  204.     *wf = f;
  205.     return (gx_xfont *)wf;
  206. }
  207. /* Map the logical font, and see if the result is satisfactory. */
  208. private int /*bool*/
  209. map_logical_font(HDC hdc, win_xfont *xf)
  210. {    char szFaceName[LF_FACESIZE];
  211.     xf->hFont = CreateFontIndirect(&xf->lf);
  212.     if ( xf->hFont == 0 )
  213.         return 0;
  214.     /* Check the face name */
  215.     SelectObject(hdc, xf->hFont);
  216.     GetTextFace(hdc, sizeof(szFaceName), szFaceName);
  217.     if ( !strncmp(xf->lf.lfFaceName, szFaceName, strlen(xf->lf.lfFaceName)) )
  218.         return 1;
  219.     DeleteObject(xf->hFont);
  220.     xf->hFont = 0;
  221.     return 0;
  222. }
  223.  
  224. /* Convert a character name or index to an xglyph code. */
  225. gx_xglyph
  226. win_char_xglyph(gx_xfont *xf, gs_char chr, int encoding_index,
  227.   gs_glyph glyph, gs_proc_glyph_name_t glyph_name_proc)
  228. {    if ( chr == gs_no_c